<script setup>
import { ref } from 'vue'
import { getData, searchData, getDataByCateId, delData } from '@/api/outBound'
import { getTableList } from '@/utils/Data'
import { getCategory } from '@/api/category'
import { ElMessage } from 'element-plus'
defineOptions({ name: 'BannerIndex' })
//定义数据存放数组
const tableData = ref(0)
//分页
const pag = ref({
  page: 1,
  pageSize: 10,
  pagetotal: 10
})
//定义类别筛选数组
const iniCategory = async () => {
  const { page, pageSize } = pag.value
  const res = await getCategory({ page, pageSize })
  CATEGORY.value = res.data.dataAll.map((item) => {
    return {
      text: item.category,
      value: item.id
    }
  })
  console.log(res)
}
iniCategory()
const CATEGORY = ref([])
//定义弹窗打开状态值
const isShow = ref(false)
//获取数据
const getList = () => {
  getTableList(getData, pag, tableData)
}
getList()
//搜索逻辑
const searchDetail = ref(null)
const search = async (title) => {
  if (!searchDetail.value) {
    getList()
    return false
  }
  const res = await searchData(searchDetail.value)
  tableData.value = res.data
  searchDetail.value = null
}
//删除逻辑
const handleDelete = async (row) => {
  const res = await delData(row.id)
  if (res.code === 20000) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
  }
  getList()
}

//分类筛选
const firstFilter = ref(true)
const categorySearch = async (value, column) => {
  if (firstFilter.value) {
    firstFilter.value = false
    if (value !== undefined) {
      if (column.property === 'category') {
        const res = await getDataByCateId(value)
        tableData.value = res.data
      }
    } else {
      getList()
    }
  }
}
const handleFilterChange = (filters) => {
  firstFilter.value = true
  if (filters.category) {
    categorySearch(filters.category[0], { property: 'category' }) // 传递正确的 column 参数
  }
}
</script>

<template>
  <div class="search">
    <el-input placeholder="搜索框" style="width: 240px"></el-input>
  </div>
  <el-table style="width: 100%" :data="tableData" @filter-change="handleFilterChange">
    <el-table-column prop="id" label="ID" width="90" />
    <el-table-column prop="title" label="货品名" width="180" />
    <el-table-column prop="username" label="操作员名称" width="180" />
    <el-table-column prop="image" label="图片" width="180">
      <template #default="{ row }">
        <img :src="row.image" alt="" width="100px" />
      </template>
    </el-table-column>
    <el-table-column prop="quantity" label="数量" width="100" />
    <el-table-column
      prop="category"
      label="分类"
      width="90"
      column-key="category"
      :filters="CATEGORY"
      :filter-method="categorySearch"
      :filter-multiple="false"
    />
    <el-table-column prop="place" label="发货地点" width="180" />
    <el-table-column prop="createTime" label="添加时间" width="180" />
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button size="small" type="danger" @click="handleDelete(row)"> 删除 </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    background
    layout="prev, pager, next"
    :total="pag.pagetotal"
    v-model:page-size="pag.pageSize"
    v-model:current-page="pag.page"
    @change="getList"
  />
</template>
<style lang="scss" scoped>
.el-form-item {
  width: 400px;
}

.w600 {
  width: 600px;
}
</style>
